﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using ComputerVision.Common;

namespace ComputerVision
{
    public partial class frmKichHoat : Form
    {
        private LicenseUtil license = null;
        private MD5 md5Hash = null;
        private frmMain main = null;
        public frmKichHoat(frmMain frm)
        {
            InitializeComponent();
            this.main = frm;
            license = new LicenseUtil();
            md5Hash = MD5.Create();
            txtMaSanPham.Text = license.ProductKey;
            txtMakichHoat.Text = license.ReadFile().Trim();

            if (license.VerifyMd5Hash(md5Hash, txtMaSanPham.Text.Trim(), txtMakichHoat.Text.Trim()))
            {
                lblDaKichHoat.Visible = true;
                picDaKichHoat.Visible = true;
                txtMakichHoat.Enabled = false;
                btKichHoat.Enabled = false;
            }
        }

        private void btKichHoat_Click(object sender, EventArgs e)
        {
            if (license.VerifyMd5Hash(md5Hash, txtMaSanPham.Text.Trim(), txtMakichHoat.Text.Trim()))
            {
                if (license.WriteFile(txtMakichHoat.Text.Trim()))
                {
                    MessageBox.Show(@"Kích hoạt thành công.", @"Thông báo", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    this.main.ShowHideMenu();
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show(@"Mã kích hoạt không hợp lệ.", @"Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMaSanPham_MouseDown(object sender, MouseEventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }
    }
}
